<template>
  <div>
    <div class="pending">
      <!-- 搜索 -->
      <el-form :inline="true" :model="query">
        <el-form-item label="分组名称">
          <el-input
            v-model.trim="query.search"
            maxlength="20"
            placeholder="请输入分组名称"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" plain @click="search">查询</el-button>
          <el-button
            type="primary"
            plain
            @click="handleCreate"
          >新增分组</el-button>
        </el-form-item>
      </el-form>
      <!-- 表格 -->
      <el-table
        v-loading="loading.table"
        :data="tableData"
        border
        :header-cell-style="{ background: '#EEF3FF', color: '#333333' }"
        tooltip-effect="dark"
        style="width: 100%"
        class="dataTable"
      >
        <el-table-column label="分组名称" width="300">
          <template #default="scope">{{ scope.row.groupName }}</template>
        </el-table-column>
        <el-table-column label="分组描述" width="300">
          <template #default="scope">{{
            scope.row.groupDescribe
          }}</template>
        </el-table-column>
        <el-table-column label="商品数量" width="150">
          <template #default="scope">{{ scope.row.number }}</template>
        </el-table-column>
        <el-table-column prop="updateTime" label="修改时间" width="220" />
        <el-table-column label="操作" show-overflow-tooltip>
          <template #default="scope">
            <el-button type="primary" link @click="handleEdit(scope.row)">编辑</el-button>
            <el-popconfirm
              title="确认删除？"
              @confirm="del(scope.row)"
            >
              <template #reference>
                <el-button type="danger" link>删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="page.page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        background
        :total="total"
        style="margin: 12px 0;"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <CommGroup ref="commGroupRef" @reload="search" />
  </div>
</template>

<script setup>
import { defineComponent, onMounted, reactive, ref, shallowRef } from 'vue'

import CommGroup from '@/views/commodity/commodityList/commodityGroup.vue'
import {
  commodityListGetAll,
  commodityListDelete,
} from '@/api/commodity'
defineComponent({
  name: 'ProductType'
})

let loading = reactive({
  table: false,
})
let visible = reactive({
  group: false,
})

let page = reactive({
  page: 1,
  pageSize: 10,
})
const total = ref(0)
let query = reactive({
  search: '',
})
let tableData = ref([]);
const commGroupRef = ref(null)
// 初始化查询所有数据
function getAll () {
  loading.table = true
  commodityListGetAll(Object.assign({}, page, query)).then(res => {
    tableData.value = res.data.list
    total.value = res.data.total
  }).finally(() => {
    loading.table = false
  })
}

function handleCurrentChange (val) {
  page.page = val
  getAll()
}
function handleSizeChange (val) {
  page.pageSize = val
  getAll()
}

//  查询
function search () {
  page.page = 1
  getAll()
}

// 编辑分组
function handleEdit (row) {
  commGroupRef.value.open(row.shopGroupId)
}

// 删除分组
function del (row) {
  ElMessageBox.confirm('选中数据将被永久删除, 是否继续？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      commodityListDelete({ shopGroupId: row.shopGroupId }).then((res) => {
        if (res.code === '') {
          ElMessage.success('删除成功')
          getAll()
        }
      })
    })
    .catch((e) => {
      console.log(e)
    })
}

// 新增分组
function handleCreate () {
  commGroupRef.value.open()
}

onMounted(() => {
  getAll()
})
</script>

<style lang="scss" scoped>
.pending {
  padding: 20px;
  margin-top: 20px;
  background-color: #FFFFFF;
}
.group-dialog {
  .btn-wrap {
    text-align: center;
  }
  .upload-wrap {
    width: 100%;
    display: flex;
    align-items: flex-end;
    .image-wrap {
      width: 120px;
      height: 120px;
      line-height: 120px;
      margin-right: 15px;
      margin-bottom: 15px;
      text-align: center;
      border: 1px dashed #d9d9d9;
      img {
        width: 100%;
        height: 100%;
      }
    }
  }
}
</style>
